var util = require('../../utils/util.js')
var request = require('../../utils/https.js')
var uri = 'order/orderlist'
const app = getApp();
Page({
  data: {
    navTab1: ["全部", "待付款", "待发货", "待收货", "已完成"],
    navTab2: ["全部", "待指派", "待确认", "待收货", "已完成"],
    navTab3: ["全部", "待指派", "待确认", "待收货", "已完成"],
    navTab4: ["全部", "待确认", "待收货", "待收货"],
    currentNavtab: 0,
    pageNo: 1,
    hidden: false,
    list: [],
    newlist: [
      {
        id:1,
        orderSn:'201807184137',
        goodsName:'七喜柠檬味碳酸汽水饮料整箱330ml*12百事可乐出品PET',
        goodsNum:'6',
        status:'待确认',
        orderTotalPrice:'600',
        goodsImage:'https://wx.weilaifw.com/public/uploads/20180714/53a1cc2434fa14d48efd6c512b284223.png',
        totalNum:6,
        totalPrice:600,
       }
    ],
    tips: '' ,
    role:wx.getStorageSync('userType'),
    showModalStatus: true,
    title:'提示',
    text:'是否确认收货!',
    showType:2,
    delivers:[
                {
                  id:1,
                  name:'战士'
                },
                {
                  id: 2,
                  name: '坦克'
                },
                {
                  id: 3,
                  name: '射手'
                }
    ],
    deliver:'选择配送员',
    showDeliver:1
  },
  onLoad: function (options) {
    // this.setData({
    //   currentNavtab: options.id,
    // })
    //刷新数据
  //this.getData();
  
  },
  //切换tab刷新数据
  switchTab: function (o) {
    var that = this;
    var idx = o.currentTarget.dataset.idx;
    if (idx !== that.data.currentNavtab) {
      that.setData({
        currentNavtab: idx,
        // list: [], //数据源清空
        // newlist: [],
        pageNo: 1
      })
      //刷新数据
      //that.getData();
    }
  },
  getData: function () {
    var that = this;
    var status = "";
    var ordertype = that.data.currentNavtab;
    if (ordertype == 0) {
      status = "";
    } else if (ordertype == 1) {
      status = "10";
    } else if (ordertype == 2) {
      status = "30";
    }
    var pageNo = that.data.pageNo;
    var CuserInfo = wx.getStorageSync('CuserInfo');
    request.req(uri, {
      ordersn: '1',
      status: status,
      pageNo: pageNo,
    }, (err, res) => {
      console.log(res.data)
      if (res.data.result == 1) {
        if (!res.data) {   //无数据
          that.setData({ hidden: true, tips: "没有数据~" })
        } else {
          that.setData({
            hidden: true,
            list: that.data.list.concat(res.data.data),
          })
          //处理数据
          var list = that.data.list;
          for (var i = 0; i < list.length; i++) {
            var item = list[i]  //状态状态
            if (item.orderState == 10) {   //待支付
              that.data.newlist.push({
                button: "去支付",
                state: "待支付",
                orderTotalPrice: item.orderTotalPrice, //总价
                goodsImage: item.orderGoodsList[0].goodsImage,//图片地址
                goodsName: item.orderGoodsList[0].goodsName,  //商品介绍介绍
                goodsNum: item.orderGoodsList[0].goodsNum  //商品数量
              });
            } else if (item.orderState == 30) {  //待收货
              that.data.newlist.push({
                button: "确认收货",
                state: "待收货",
                orderTotalPrice: item.orderTotalPrice, //总价
                goodsImage: item.orderGoodsList[0].goodsImage,//图片地址
                goodsName: item.orderGoodsList[0].goodsName,  //商品介绍介绍
                goodsNum: item.orderGoodsList[0].goodsNum  //商品数量
              });
            } else {
              that.data.newlist.push({
                button: "查看详情",
                state: "其他状态",
                orderTotalPrice: item.orderTotalPrice, //总价
                goodsImage: item.orderGoodsList[0].goodsImage,//图片地址
                goodsName: item.orderGoodsList[0].goodsName,  //商品介绍介绍
                goodsNum: item.orderGoodsList[0].goodsNum  //商品数量
              });
            }
          }
          console.log(that.data.newlist);
          that.setData({
            newlist: that.data.newlist
          })
        }
      }
    })
  },
  //点击到相应的页面
  orderbutton: function (options) {
    var type = options.currentTarget.dataset.button;
    console.log(type)
    if (type == "去支付") {

    } else if (type == "查看详情") {

    } else if (type == "确认收货") {

    }
  },

  //下滑加载更多
  lower: function () {
    console.log("下滑啦");
    var that = this;
    that.setData({ pageNo: that.data.pageNo + 1 })
    //that.getData();
  },
   /* 关闭弹窗 */
  closeModal:function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  }
})